package cn.iocoder.yudao.module.promotion.gift.controller.admin;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.controller.CrudController;
import cn.iocoder.yudao.framework.business.basic.controller.ListController;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.datapermission.core.util.DataPermissionUtils;
import cn.iocoder.yudao.module.api.promotion.gift.dto.GiftActivityVO;
import cn.iocoder.yudao.module.api.shop.admin.ShopUserApi;
import cn.iocoder.yudao.module.promotion.gift.pojo.dto.AdminGiftActivityQueryDTO;
import cn.iocoder.yudao.module.promotion.gift.pojo.dto.AdminGiftActivitySaveDTO;
import cn.iocoder.yudao.module.promotion.gift.service.admin.GiftActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 赠品活动")
@RestController
@RequestMapping("/promotion/gift/activity")
@Validated
public class GiftActivityController implements CrudController<GiftActivityVO, AdminGiftActivitySaveDTO, AdminGiftActivityQueryDTO>, ListController<GiftActivityVO, AdminGiftActivityQueryDTO> {

    @Resource
    private GiftActivityService giftActivityService;

    @Resource
    private ShopUserApi shopUserApi;

    @PreAuthorize("@ss.hasAnyRoles('shop_admin')")
    @Override
    public CommonResult<Long> create(AdminGiftActivitySaveDTO dto) {
        return success(giftActivityService.create(dto));
    }

    @PreAuthorize("@ss.hasAnyRoles('shop_admin')")
    @Override
    public CommonResult<Boolean> update(AdminGiftActivitySaveDTO updateReqVO) {
        return success(giftActivityService.update(updateReqVO));
    }

    @PreAuthorize("@ss.hasAnyRoles('shop_admin')")
    @Override
    public CommonResult<Boolean> delete(Long id) {
        return success(giftActivityService.delete(id));
    }

    @PreAuthorize("@ss.hasPermission('promotion:gift:activity')")
    @Override
    public CommonResult<GiftActivityVO> getDetail(Long id) {
        return success(giftActivityService.getDetail(id));
    }

    @PreAuthorize("@ss.hasPermission('promotion:gift:activity')")
    @Override
    public CommonResult<PageResult<GiftActivityVO>> getPage(Integer pageNo, Integer pageSize, AdminGiftActivityQueryDTO param) {
        if ((param.getPartnerShopId() != null && param.getPartnerShopId() > 0) || CollUtil.isNotEmpty(param.getIds())) {
            // 忽略数据权限，允许获取合作的店铺的赠品活动信息
            return success(DataPermissionUtils.executeIgnore(() -> giftActivityService.getPage(pageNo, pageSize, param)));
        }
        return success(giftActivityService.getPage(pageNo, pageSize, param));
    }

    @PreAuthorize("@ss.hasPermission('promotion:gift:activity')")
    @Override
    public CommonResult<List<GiftActivityVO>> getList(AdminGiftActivityQueryDTO param) {
        if ((param.getPartnerShopId() != null && param.getPartnerShopId() > 0) || CollUtil.isNotEmpty(param.getIds())) {
            // 忽略数据权限，允许获取合作的店铺的赠品活动信息
            return success(DataPermissionUtils.executeIgnore(() -> giftActivityService.getList(param)));
        }
        return success(giftActivityService.getList(param));
    }

    @PreAuthorize("@ss.hasAnyRoles('shop_admin')")
    @Operation(summary = "列表（供选择）")
    @GetMapping("/list-for-pick")
    public CommonResult<List<GiftActivityVO>> getListForPick() {
        Set<Long> shopIds = shopUserApi.getShopIdsByUserId(getLoginUserId(), UserTypeEnum.ADMIN.getValue());
        Long shopId = shopIds.stream().sorted().toList().get(shopIds.size() - 1);
        return success(giftActivityService.getAvailableListByShopId(shopId));
    }

}
